package com.client.utils;

import com.client.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

@Component
//UDP工具类
public class UDPUtil {
    @Value("${udp.send}")
    private Integer send_port;

    @Value("${udp.receive}")
    private Integer receive_port;

    @Autowired
    MessageService messageService;

    //发送UDP包
    public void send(String goal_ip, int goal_port, byte[] bytes) {
        try {
            DatagramSocket datagramPacket = new DatagramSocket();
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(goal_ip), goal_port);
            datagramPacket.send(packet);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
